package fr.montreuil.wargame.service;

/**********************************
 ***	Imports
 **********************************/
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.montreuil.wargame.domain.Batiment;
import fr.montreuil.wargame.domain.Technologie;
import fr.montreuil.wargame.domain.User;
import fr.montreuil.wargame.repository.UserRepository;

/**********************************
 *** 	@jgay
 *** 	@25 avr. 2012
 **********************************/
@Service
@Transactional
public class UserService {
	/**********************************
	 ***	Variables
	 **********************************/
	@Autowired
	private UserRepository userRepository;

	/**********************************
	 ***	Méthodes
	 **********************************/
	 @Scheduled(fixedDelay=30*1000)
	 //@Scheduled(fixedRate=5000)
	 //@Scheduled(cron="*/5 * * * * ?")
	 public void doSchedule() {
		 
		 int ress1 = 0;
		 int ress2 = 0;
		 int ress3 = 1;
		 int ress4 = 10;
		 
		 List<User> users = userRepository.findAll();
		 
		 for(User user: users){
			 for(Batiment batiment : user.getBatiments()){
				 ress1 = ress1 + batiment.getInterestResources1();
				 ress2 = ress2 + batiment.getInterestResources2();
				 ress3 = ress3 + batiment.getInterestResources3();
				 ress4 = ress4 + batiment.getInterestResources4();
			 }
			 
			 for(Technologie technologie : user.getTechnologies()){
				 ress1 = ress1 + technologie.getInterestResources1();
				 ress2 = ress2 + technologie.getInterestResources2();
				 ress3 = ress3 + technologie.getInterestResources3();
				 ress4 = ress4 + technologie.getInterestResources4();
			 }
			 
			 ress1 =  user.getResources().getResources1() + ress1;
			 ress2 =  user.getResources().getResources2() + ress2;
			 ress3 =  user.getResources().getResources3() + ress3;
			 ress4 =  user.getResources().getResources4() + ress4;
			 
			 user.getResources().setResources1(ress1);
			 user.getResources().setResources2(ress2);
			 user.getResources().setResources3(ress3);
			 user.getResources().setResources4(ress4);
			 
			 userRepository.save(user);
		 }
	 }

}
